package com.sugon.controller.spare;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.SparePartBackEntity;
import com.sugon.entity.SparePartOutEntity;
import com.sugon.modules.sparepart.service.ISparepartManager;
import com.sugon.service.SparePartBackService;
import com.sugon.service.SparePartOutService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 退库记录Controller
 *
 * @author sugon
 * @date 2019-07-19 16:08:15
 */
@Controller
@RequestMapping("sparepartback")
public class SparePartBackController {

    @Autowired
    private SparePartBackService sparePartBackService;
    @Autowired
    private ISparepartManager sparepartManager;
    @Autowired
    private SparePartOutService sparePartOutService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sparepartback:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<SparePartBackEntity> sparePartBackList = sparePartBackService.queryList(query);
        int total = sparePartBackService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sparePartBackList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sparepartback:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        SparePartBackEntity sparePartBack = sparePartBackService.queryObject(id);

        return R.ok().put("sparePartBack", sparePartBack);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sparepartback:save")
    @ResponseBody
    public R save(@RequestBody SparePartBackEntity sparePartBack) {
        // 校验出库单号是否存在
        SparePartOutEntity sparePartOut = sparePartOutService.queryObject(sparePartBack.getOutId());
        if (sparePartOut == null) {
            return R.error("备件出库单号不存在，请确认！");
        }
        // 校验是否可退库
        int relNum = sparePartOut.getRelNum();
        if (relNum == 0 || relNum < sparePartBack.getBackNum()) {
            return R.error("当前备件出库单可退库数不足，无法执行退库！");
        }
        sparePartBack.setSparePartOut(sparePartOut);
        return sparepartManager.cancelStock(sparePartBack) ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sparepartback:update")
    @ResponseBody
    public R update(@RequestBody SparePartBackEntity sparePartBack) {
        sparePartBack.setUpdateUser(getUserId());
        sparePartBack.setUpdateDate(new Date());
        sparePartBackService.update(sparePartBack);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sparepartback:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        sparePartBackService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SparePartBackEntity> list = sparePartBackService.queryList(params);

        return R.ok().put("list", list);
    }

}
